package com.tju320.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tju320.elm.dao.FoodDao;
import com.tju320.elm.po.Business;
import com.tju320.elm.po.Food;
import com.tju320.elm.util.DBUtil;

public class FoodDaoImpl implements FoodDao {

	@Override
	public List<Food> listFoodByBusinessId(Integer businessId) {
		// 通过列表是否为空判断是否存在这样的元素
		List<Food> list = new ArrayList<Food>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstm = null;
		ResultSet rs = null;
		// 3.准备发送SQL
		String sql = "select * from food where businessId=? order by foodId";
		
		try {
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, businessId);
			// 执行sql
			rs = pstm.executeQuery();
			// 可能有多个结果，装载list里返回
			while (rs.next()) {
				Food food = new Food();
				food.setBusinessId(rs.getInt("businessId"));
				food.setFoodExplain(rs.getString("foodExplain"));
				food.setFoodId(rs.getInt("foodId"));
				food.setFoodName(rs.getString("foodName"));
				food.setFoodPrice(rs.getDouble("foodPrice"));
				list.add(food);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstm, conn);
		}

		return list;

	}

	@Override
	public int saveFood(Food food) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int foodId=0;
		String sql="insert into food (foodName,foodExplain,foodPrice,businessId) values (?,?,?,?)";
		try {
			pstm=conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			pstm.setString(1, food.getFoodName());
			pstm.setString(2, food.getFoodExplain());
			pstm.setDouble(3, food.getFoodPrice());
			pstm.setInt(4, food.getBusinessId());
			pstm.executeUpdate();
			rs=pstm.getGeneratedKeys();
			if(rs.next()) {
				foodId=rs.getInt(1);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtil.close(rs, pstm, conn);
		}
		return foodId;
	}

	@Override
	public Food getFoodById(Integer foodId) {
		Food food = null;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstm = null;
		ResultSet rs = null;
		// 3.准备发送SQL
		String sql = "select * from food where foodId=?";
		try {
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, foodId);
			// 执行sql
			rs = pstm.executeQuery();
			// 可能有多个结果，装载list里返回
			if (rs.next()) {
				food=new Food();
				food.setBusinessId(rs.getInt("businessId"));
				food.setFoodExplain(rs.getString("foodExplain"));
				food.setFoodId(rs.getInt("foodId"));
				food.setFoodName(rs.getString("foodName"));
				food.setFoodPrice(rs.getDouble("foodPrice"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstm, conn);
		}
		return food;	
	}

	@Override
	public int updateFood(Food food) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int result=0;
		// 3.准备发送SQL
		String sql = "update food set foodName=?,foodExplain=?,foodPrice=? where foodId=?";
		try {
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, food.getFoodName());
			pstm.setString(2, food.getFoodExplain());
			pstm.setDouble(3, food.getFoodPrice());
			pstm.setInt(4, food.getFoodId());
			
			// 执行sql
			result=pstm.executeUpdate();
			// 可能有多个结果，装载list里返回
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstm, conn);
		}
		return result;	
		
	}

	@Override
	public int removeFood(Integer foodId) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int result=0;
		// 3.准备发送SQL
		String sql = "delete from food where foodId=?";
		try {
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, foodId);
			// 执行sql
			result=pstm.executeUpdate();
			// 可能有多个结果，装载list里返回
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstm, conn);
		}
		return result;	
	}

}
